<?php

/**
 * 购物车模块控制器类相关
 * @author:Samuel
 * @since:2012 5.5
 */
class ShoppingController extends BaseController {

    /**
     * 添加记录
     * @param $productId int 产品id
     * @param $num int 数量
     * @return array
     * @throws AppException 
     */
    public function addShopping() {
        //商品Id
        $productId = $this->getParamInt('PId', true, true);
        //数量
        $num = $this->getParamInt('number', true);
        if ($productId <= 0 || $num <= 0) {
            throw new AppException(100);
        }
        /**
         * 添加购物车
         */
        
        //若用户已登录，同步session
        if (isset($_SESSION['UId'])) {
            Shopping_Model::getInstance($_SESSION['UId'])->increaseShopping($productId, $num);
            if ( !empty($_SESSION['shopping'] ) ) {
                $arrShopping =  unserialize ( $_SESSION['shopping'] );
                foreach ( $arrShopping as $productId => $num) {
                    Shopping_Model::getInstance($_SESSION['UId'])->increaseShopping($productId, $num);
                }
                unset($_SESSION['shopping']);
            }
            
           $_SESSION['shopping111']['list'] = serialize(Shopping_Model::getInstance($_SESSION['UId'])->getShoppingList());
        } 
        else {
            //写入session
            if (isset($_SESSION['shopping'])) {
                 $array = unserialize( $_SESSION['shopping'] );
                 //print_r($array);
                if ( array_key_exists ($productId , $array ) ) {
                    $nums = $array[$productId]+$num;
                    $array[$productId] = $nums;
                    $arr = serialize($array);
                     $_SESSION['shopping'] = $arr;
                } 
                else {
                    $array[$productId]= $num;
                    $arr = serialize( $array );
                    //$_SESSION( 'shopping' , $arr , time()+3600);
                    $_SESSION['shopping'] = $arr;
                }
            }
            else {
                $array = array($productId => $num);
                $array = serialize($array);
                $_SESSION['shopping'] = $array;
            }
        }
     
        /**
         * 获取购物车中产品的详细信息
         */
        if( isset( $_SESSION['shopping'])){
            $cartarr = unserialize($_SESSION['shopping']);
            $arrPId= array_keys( $cartarr );
            $cartInfo = Product_Info::getInstance()->getProductInfoMulti( $arrPId );  
        }
        else{
            $cartInfo = Product_Info::getInstance()->getProuctInfoList( $productId ); 
        }
        include TPL_DIR . 'cart.php';
    }
//
//    /**
//     * 获取购物车
//     * @return array 
//     */
//    public function getShopping() {
//        
//        if (isset($_SESSION['UId'])) {
//            echo 2222;
//            $_COOKIE['shopping']['list'] = Shopping_Model::getInstance($_SESSION['UId'])->getShoppingList();
//        } else {
//            if (isset($_COOKIE['shopping'])) {
//               $cartarr = unserialize($_COOKIE['shopping']);
//               $arrPId= array_keys( $cartarr );
//               $cartInfo = Product_Info::getInstance()->getProductInfoMulti( $arrPId );  
//            } else {
//            }
//        }
//         include TPL_DIR . 'cart.php';
//    }
//
//    /**
//     * 删除记录
//     * @param $productId 产品id
//     * @param $num 数量
//     * @return array
//     * @throws AppException 
//     */
//    public function removeShopping() {
//        //商品Id
//        $productId = $this->getParamInt('PId', true, true);
//        //数量
//        //$num = $this->getParamInt( 'number' , true );
//        //数量
//        //$num = $this->getParamInt( 'num' , true );
////        if( $productId <= 0 || $num <= 0 )
////        {
////            throw new AppException( 100 );
////        }
//
//        if (isset($_SESSION['UId'])) {
//            echo 111;
//            Shopping_Model::getInstance($_SESSION['UId'])->decreaseShopping($productId, $num);
//        } else {
//            if (isset($_COOKIE['shopping']['new'][$productId])) {
//                if ($num <= $_COOKIE['shopping']['new'][$productId]) {
//                    $_COOKIE['shopping']['new'][$productId] -= $num;
//                    if ($_COOKIE['shopping']['new'][$productId] == 0) {
//                        unset($_COOKIE['shopping']['new'][$productId]);
//                    }
//                } else {
//                    $num -= $_COOKIE['shopping']['new'][$productId];
//                    unset($_COOKIE['shopping']['new'][$productId]);
//
//                    if (isset($_COOKIE['shopping']['list'][$productId])) {
//                        $_COOKIE['shopping']['list'][$productId] -= $num;
//                        if ($_COOKIE['shopping']['list'][$productId] <= 0) {
//                            unset($_COOKIE['shopping']['list'][$productId]);
//                        }
//                    }
//                }
//            }
//        }
//        include TPL_DIR . 'cart.php';
//    }
//
//    /**
//     * 清空购物车
//     * @return array 
//     */
//    public function cleanShopping() {
//        if (isset($_SESSION['UId'])) {
//            Shopping_Model::getInstance($_SESSION['UId'])->cleanShopping();
//        }
//        unset($_COOKIE['shopping']);
//        return $_COOKIE['shopping'];
//    }

}

?>